package org.example.examsystem.controller;



import org.example.examsystem.entity.Subject;
import org.example.examsystem.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/questions")
@CrossOrigin(origins = "http://localhost:5173") // 允许前端端口跨域
public class SubjectController {

    @Autowired
    private SubjectService subjectService;

    @GetMapping
    public Page<Subject> getQuestions(@RequestParam int page, @RequestParam int size, @RequestParam int stype) {
        return subjectService.getQuestionsByPage(page, size, stype);
    }

    @PostMapping
    public void insertQuestion(@RequestBody Subject subject) {
        subjectService.insertQuestion(subject);
    }

    @PutMapping("/{sid}")
    public void updateQuestion(@PathVariable int sid, @RequestBody Subject subject) {
        subject.setSid(sid);
        subjectService.updateQuestion(subject);
    }

    @DeleteMapping("/{sid}")
    public void deleteQuestion(@PathVariable int sid) {
        subjectService.deleteQuestion(sid);
    }

    @DeleteMapping("/batch")
    public void batchDeleteQuestions(@RequestBody List<Integer> ids) {
        subjectService.batchDeleteQuestions(ids);
    }

    // 添加 POST 请求处理方法
    @PostMapping("/batch")
    public void batchDeleteQuestionsByPost(@RequestBody List<Integer> ids) {
        subjectService.batchDeleteQuestions(ids);
    }

    @GetMapping("/random")
    public List<Subject> getRandomSubjects(@RequestParam int cno, @RequestParam int stype, @RequestParam int count) {
        return subjectService.getRandomSubjects(cno, stype, count);
    }

    @GetMapping("/{sid}")
    public Subject getSubjectById(@PathVariable int sid) {
        return subjectService.getSubjectById(sid);
    }
}